import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { BossAreaList } from "./item/BossAreaList";
import { BossAreaItem } from "./item/BossAreaItem";
import { Global } from "../../../../com/gengine/global/Global";
import { SBossPoints } from "../../../../Message/Public/SBossPoints";
import { ObjectPool } from "../../../../com/gengine/utils/pools/ObjectPool";
import { EntityPoint } from "./point/EntityPoint";
import { GameMapUtil } from "../../scene/map/GameMapUtil";
import { Cache } from "../../cache/Cache";
import { Game } from "../../Game";
import { EventName } from "../../mvc/EventName";
import { NPCItem } from "./item/NPCItem";
import { BossItem } from "./item/BossItem";
import { PassItem } from "./item/PassItem";
import { SceneInfo } from "../../scene/map/SceneInfo";
import { BossRule } from "../../rules/BossRule";
import { PassPointRule } from "../../rules/PassPointRule";
import { RoleItem } from "./item/RoleItem";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { DataEvent } from "../../events/DataEvent";
import { NPCInfo } from "../../model/NPCInfo";
import { TaskTargetInfo } from "../../model/TaskTargetInfo";
import { EntityType } from "../../rules/EntityType";
import { MapFileUtil } from "../../scene/map/MapFileUtil";
import { AIManager } from "../../scene/ai/AIManager";
import { FlyFootManager } from "../../manager/FlyFootManager";
import { BossRefreshInfo } from "../../model/BossRefreshInfo";
import { SPassPoint } from "../../../../Message/Public/SPassPoint";
import { AIType } from "../../scene/ai/AIType";
type int = number;
//class ItemLayer
    
    export  class ItemLayer /* flash.display.Sprite */
    {
        constructor(arg1: Boolean)
        {
            
            this._isCurMap = arg1;
            this._npcItems = [];
            this._bossItems = [];
            this._passItems = [];
            this._areaItems = [];
            this._bossPoints = [];
            this._target = new Object /* flash.display.Bitmap */();
            LoaderHelp.addResCallBack(ResFileConst.mapUI, this.onMapUIResLoadedHandler);
            return;
        }

        /* internal  */onHideAreaList(): void
        {
            BossAreaList.getInstance().dispose();
            return;
        }

        public setTargetPosition(arg1: number, arg2: number): void
        {
            this._target.x = arg1;
            this._target.y = arg2 - this._target.height;
            if (this.contains(this._target) == false) 
            {
                this.addChild(this._target);
            }
            this.reSetChildIndex();
            return;
        }

        protected reSetChildIndex(): void
        {
            if (this._target && this.contains(this._target) && !(this.getChildIndex(this._target) == numChildren - 2)) 
            {
                this.setChildIndex(this._target, (numChildren - 1));
            }
            if (this._role && this.contains(this._role) && !(this.getChildIndex(this._role) == (numChildren - 1))) 
            {
                this.setChildIndex(this._role, (numChildren - 1));
            }
            return;
        }

        protected resetNpcChildIndex(): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            if (this._npcItems.length >= 1) 
            {
                loc2 = this._npcItems.length;
                while (loc1 < loc2) 
                {
                    loc3 = this._npcItems[loc1];
                    addChild(loc3);
                    ++loc1;
                }
            }
            return;
        }

        /* internal  */onAreaMouseOverHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            flash.utils.clearTimeout(this._timeKey);
            var loc1=arg1.target as BossAreaItem;
            loc1.addEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onAreaMouseOutHandler);
            if (loc1.data.boss.length > 0) 
            {
                loc2 = BossAreaList.getInstance();
                loc2.updateData(loc1.data);
                loc2.x = mouseX;
                loc2.y = mouseY;
                if ((loc3 = localToGlobal(new Object /* flash.geom.Point */(loc2.x, loc2.y))).y + loc2.height > Global.stage.stageHeight - 10) 
                {
                    loc3.y = Global.stage.stageHeight - loc2.height;
                    loc4 = globalToLocal(loc3);
                    loc2.y = loc4.y - 10;
                }
                addChild(loc2);
                loc2.addEventListener(flash.events.MouseEvent.MOUSE_OVER, this.onAreaListMouseOver);
            }
            else 
            {
                BossAreaList.getInstance().dispose();
            }
            return;
        }

        public setRolePosition(arg1: number, arg2: number): void
        {
            this._roleX = arg1;
            this._roleY = arg2;
            if (this._role && this._role.parent) 
            {
                this._role.x = arg1 - this._role.width / 2;
                this._role.y = arg2 - this._role.height / 2;
            }
            return;
        }

        public updateBossPoint(arg1: SBossPoints, arg2: number, arg3: number): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            this.cleanBossPoint();
            var loc2=arg1.points.length;
            while (loc1 < loc2) 
            {
                loc3 = arg1.points[loc1];
                (loc4 = ObjectPool.getObject(EntityPoint)).updateData(null, 16711680, 2);
                loc4.mouseChildren = false;
                loc4.mouseEnabled = false;
                this._bossPoints.push(loc4);
                loc5 = GameMapUtil.tilePointToLocal(loc3.x, loc3.y, arg2, arg3);
                loc4.updatePosition(loc5.x, loc5.y);
                addChildAt(loc4, 0);
                ++loc1;
            }
            return;
        }

        public updateBossRefreshPoint(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=null;
            if (Cache.instance.mapui.showBoss && this._showingBoss) 
            {
                var loc3=0;
                var loc4=this._bossItems;
                for(loc2 of loc4) 
                {
                    loc1 = Game.sceneInfo.getBossRefreshInfoByCode(loc2.bossCode);
                    if (!loc1) 
                    {
                        continue;
                    }
                    loc2.updateData(loc1);
                }
            }
            return;
        }

        public updatePassPoint(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=this._passItems;
            for(loc1 of loc3) 
            {
                loc1.updateData();
            }
            return;
        }

        public cleanNPC(): void
        {
            var loc1=0;
            var loc3=null;
            var loc2=this._npcItems.length;
            while (loc1 < loc2) 
            {
                loc3 = this._npcItems[loc1];
                if (this.contains(loc3)) 
                {
                    this.removeChild(loc3);
                }
                loc3.dispose();
                loc3.removeEventListener(EventName.NpcItemClick, this.onNpcItemClickHandler);
                ObjectPool.disposeObject(loc3, NPCItem);
                ++loc1;
            }
            this._npcItems.splice(0);
            this._showingNpc = false;
            return;
        }

        protected cleanBoss(): void
        {
            var loc1=0;
            var loc3=null;
            var loc2=this._bossItems.length;
            while (loc1 < loc2) 
            {
                loc3 = this._bossItems[loc1];
                if (this.contains(loc3)) 
                {
                    this.removeChild(loc3);
                }
                loc3.dispose();
                loc3.removeEventListener(EventName.BossItemClick, this.onBossItemClickHandler);
                ObjectPool.disposeObject(loc3, BossItem);
                ++loc1;
            }
            this._bossItems.splice(0);
            this._showingBoss = false;
            return;
        }

        protected cleanPass(): void
        {
            var loc1=0;
            var loc3=null;
            var loc2=this._passItems.length;
            while (loc1 < loc2) 
            {
                loc3 = this._passItems[loc1];
                if (this.contains(loc3)) 
                {
                    this.removeChild(loc3);
                }
                loc3.dispose();
                loc3.removeEventListener(EventName.PassItemClick, this.onPassItemClickHandler);
                ObjectPool.disposeObject(loc3, PassItem);
                ++loc1;
            }
            this._passItems.splice(0);
            this._showingPass = false;
            return;
        }

        protected cleanArea(): void
        {
            var loc1=0;
            var loc3=null;
            var loc2=this._areaItems.length;
            while (loc1 < loc2) 
            {
                loc3 = this._areaItems[loc1];
                if (this.contains(loc3)) 
                {
                    this.removeChild(loc3);
                }
                loc3.dispose();
                loc3.removeEventListener(flash.events.MouseEvent.MOUSE_OVER, this.onAreaMouseOverHandler);
                loc3.removeEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onAreaMouseOutHandler);
                ObjectPool.disposeObject(loc3, BossAreaItem);
                ++loc1;
            }
            this._areaItems.splice(0);
            this._showingArea = false;
            return;
        }

        public cleanTarget(): void
        {
            if (this.contains(this._target)) 
            {
                removeChild(this._target);
            }
            return;
        }

        public clean(): void
        {
            this.cleanNPC();
            this.cleanPass();
            this.cleanBoss();
            this.cleanArea();
            this.cleanTarget();
            this.removeLocation();
            return;
        }

        public updateSceneUnit(arg1: number, arg2: number, arg3: SceneInfo, arg4: Boolean=true): void
        {
            var loc1=0;
            var loc2=0;
            var loc3=null;
            var loc4=null;
            var loc5=null;
            var loc6=null;
            var loc7=null;
            var loc8=null;
            var loc9=null;
            var loc10=null;
            var loc11=null;
            this._scaleWidth = arg1;
            this._scaleHeight = arg2;
            this._sceneInfo = arg3;
            if (Cache.instance.mapui.showNpc) 
            {
                if (!this._showingNpc) 
                {
                    loc1 = 0;
                    loc2 = arg3.npcInfos.length;
                    while (loc1 < loc2) 
                    {
                        if ((loc4 = arg3.npcInfos[loc1]).snpc.notShowInMap == 1 || !loc4.isAddStage || loc4.minShowLevel > Cache.instance.role.curWorldLevel || loc4.maxShowLevel < Cache.instance.role.curWorldLevel) 
                        {
                            ++loc1;
                            continue;
                        }
                        loc3 = GameMapUtil.tilePointToLocal(loc4.snpc.point.x, loc4.snpc.point.y, arg1, arg2);
                        (loc5 = ObjectPool.getObject(NPCItem)).x = loc3.x - loc5.width / 2;
                        loc5.y = loc3.y - loc5.height;
                        loc5.data = loc4;
                        this._npcItems.push(loc5);
                        loc5.addEventListener(EventName.NpcItemClick, this.onNpcItemClickHandler);
                        ++loc1;
                    }
                    this._showingNpc = true;
                    this._npcItems.sortOn(["y"], Array.NUMERIC);
                    this.resetNpcChildIndex();
                }
            }
            else if (this._showingNpc) 
            {
                this.cleanNPC();
            }
            if (Cache.instance.mapui.showBoss) 
            {
                if (!this._showingBoss) 
                {
                    loc1 = 0;
                    loc2 = arg3.bossRefreshs.length;
                    while (loc1 < loc2) 
                    {
                        loc6 = arg3.bossRefreshs[loc1];
                        if (!BossRule.isShowInMapUI(loc6, this._sceneInfo.sMapDefine.mapId)) 
                        {
                            ++loc1;
                            continue;
                        }
                        loc3 = GameMapUtil.tilePointToLocal(loc6.px, loc6.py, arg1, arg2);
                        (loc7 = ObjectPool.getObject(BossItem)).updateData(loc6);
                        loc7.x = loc3.x;
                        loc7.y = loc3.y;
                        this._bossItems.push(loc7);
                        addChildAt(loc7, 0);
                        loc7.addEventListener(EventName.BossItemClick, this.onBossItemClickHandler);
                        ++loc1;
                    }
                    this._showingBoss = true;
                }
            }
            else if (this._showingBoss) 
            {
                this.cleanBoss();
            }
            if (Cache.instance.mapui.showPass) 
            {
                if (!this._showingPass) 
                {
                    loc1 = 0;
                    loc2 = arg3.passInfos.length;
                    while (loc1 < loc2) 
                    {
                        loc8 = arg3.passInfos[loc1];
                        if (PassPointRule.canISeePassPoint(loc8, true)) 
                        {
                            loc3 = GameMapUtil.tilePointToLocal(loc8.point.x, loc8.point.y, arg1, arg2);
                            (loc9 = ObjectPool.getObject(PassItem)).data = loc8;
                            loc9.x = loc3.x - loc9.width / 2;
                            loc9.y = loc3.y - loc9.height / 2;
                            this._passItems.push(loc9);
                            addChild(loc9);
                            loc9.addEventListener(EventName.PassItemClick, this.onPassItemClickHandler);
                        }
                        ++loc1;
                    }
                    this._showingPass = true;
                }
            }
            else if (this._showingPass) 
            {
                this.cleanPass();
            }
            if (Cache.instance.mapui.showArea) 
            {
                if (!this._showingArea) 
                {
                    loc1 = 0;
                    loc2 = arg3.bossArea.length;
                    while (loc1 < loc2) 
                    {
                        loc10 = arg3.bossArea[loc1];
                        loc3 = GameMapUtil.tilePointToLocal(loc10.px, loc10.py, arg1, arg2);
                        (loc11 = ObjectPool.getObject(BossAreaItem)).data = loc10;
                        loc11.x = loc3.x - loc11.width / 2;
                        loc11.y = loc3.y - loc11.height / 2;
                        this._areaItems.push(loc11);
                        addChildAt(loc11, 0);
                        ++loc1;
                    }
                    this._showingArea = true;
                }
            }
            else if (this._showingArea) 
            {
                this.cleanArea();
            }
            if (arg4) 
            {
                if (!this._role) 
                {
                    this._role = new RoleItem();
                }
                addChild(this._role);
                this.setRolePosition(this._roleX, this._roleY);
            }
            else if (this._role && this._role.parent) 
            {
                removeChild(this._role);
            }
            this.reSetChildIndex();
            return;
        }

        public targetLocation(arg1: int, arg2: int, arg3: int, arg4: number, arg5: number): void
        {
            if (!this._targetLocation) 
            {
                this._targetLocation = ObjectPool.getObject(FlashMask);
                this._targetLocation.mouseChildren = false;
                this._targetLocation.mouseEnabled = false;
            }
            var loc1=GameMapUtil.tilePointToLocal(arg2, arg3, arg4, arg5);
            this._targetLocation.x = loc1.x;
            this._targetLocation.y = loc1.y;
            if (this._targetLocation.parent != this) 
            {
                this.addChild(this._targetLocation);
            }
            return;
        }

        protected onMapUIResLoadedHandler(): void
        {
            this._target.bitmapData = GlobalClass.getBitmapData(ImagesConst.MapUITarget);
            return;
        }

        /* internal  */onNpcItemClickHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as NPCInfo;
            if (loc1.tnpc == null) 
            {
                return;
            }
            var loc2=new TaskTargetInfo();
            loc2.x = loc1.snpc.point.x;
            loc2.y = loc1.snpc.point.y;
            loc2.id = loc1.tnpc.npcId;
            loc2.mapId = this._sceneInfo.sMapDefine.mapId;
            loc2.targetType = EntityType.NPC;
            if (this._isCurMap) 
            {
                loc2.proxyId = MapFileUtil.proxyID;
                loc2.serverId = MapFileUtil.serverID;
            }
            AIManager.onAutoPathAIControl(loc2);
            var loc3=GameMapUtil.tilePointToLocal(loc2.x, loc2.y, this._scaleWidth, this._scaleHeight);
            FlyFootManager.showFlyFoot(this, loc3.x - 20, loc3.y - 20, loc2.x, loc2.y, loc2.mapId, loc2.id, EntityType.NPC, loc2.proxyId, loc2.serverId);
            return;
        }

        /* internal  */onBossItemClickHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as BossRefreshInfo;
            var loc2=new TaskTargetInfo();
            loc2.x = loc1.px;
            loc2.y = loc1.py;
            loc2.id = loc1.getNormalBoss().code;
            loc2.name = loc1.getNormalBoss().name;
            loc2.mapId = this._sceneInfo.sMapDefine.mapId;
            loc2.targetType = EntityType.Boss;
            if (this._isCurMap) 
            {
                loc2.proxyId = MapFileUtil.proxyID;
                loc2.serverId = MapFileUtil.serverID;
            }
            AIManager.onAutoPathAIControl(loc2);
            var loc3=GameMapUtil.tilePointToLocal(loc2.x, loc2.y, this._scaleWidth, this._scaleHeight);
            FlyFootManager.showFlyFoot(this, loc3.x - 20, loc3.y - 20, loc2.x, loc2.y, loc2.mapId, loc2.id, EntityType.Boss, loc2.proxyId, loc2.serverId);
            return;
        }

        /* internal  */onPassItemClickHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPassPoint;
            var loc2=new TaskTargetInfo();
            loc2.id = loc1.passPointId;
            loc2.mapId = this._sceneInfo.sMapDefine.mapId;
            loc2.x = loc1.point.x;
            loc2.y = loc1.point.y;
            loc2.targetType = EntityType.Pass;
            if (this._isCurMap) 
            {
                loc2.proxyId = MapFileUtil.proxyID;
                loc2.serverId = MapFileUtil.serverID;
            }
            AIManager.onAutoPathAIControl(loc2, AIType.AI_Scene);
            var loc3=GameMapUtil.tilePointToLocal(loc2.x, loc2.y, this._scaleWidth, this._scaleHeight);
            FlyFootManager.showFlyFoot(this, loc3.x - 20, loc3.y - 20, loc2.x, loc2.y, loc2.mapId, loc2.id, EntityType.Pass, loc2.proxyId, loc2.serverId);
            return;
        }

        public removeLocation(): void
        {
            if (this._targetLocation) 
            {
                if (this.contains(this._targetLocation)) 
                {
                    removeChild(this._targetLocation);
                }
                ObjectPool.disposeObject(this._targetLocation, FlashMask);
                this._targetLocation = null;
            }
            return;
        }

        public cleanBossPoint(): void
        {
            var loc1=0;
            var loc3=null;
            var loc2=this._bossPoints.length;
            while (loc1 < loc2) 
            {
                loc3 = this._bossPoints[loc1];
                if (this.contains(loc3)) 
                {
                    this.removeChild(loc3);
                    loc3.dispose();
                    ObjectPool.disposeObject(loc3);
                }
                ++loc1;
            }
            this._bossPoints.splice(0);
            return;
        }

        /* internal  */onAreaMouseOutHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as BossAreaItem;
            loc1.removeEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onAreaMouseOutHandler);
            this._timeKey = flash.utils.setTimeout(this.onHideAreaList, 300);
            return;
        }

        /* internal  */onAreaListMouseOver(arg1: Object /* flash.events.MouseEvent */): void
        {
            BossAreaList.getInstance().addEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onAreaListMouseOut);
            flash.utils.clearTimeout(this._timeKey);
            return;
        }

        /* internal  */onAreaListMouseOut(arg1: Object /* flash.events.MouseEvent */): void
        {
            BossAreaList.getInstance().removeEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onAreaListMouseOut);
            this._timeKey = flash.utils.setTimeout(this.onHideAreaList, 300);
            return;
        }

        /* internal */ /* var */_isCurMap: Boolean;

        /* internal */ /* var */_npcItems: Array<any>;

        /* internal */ /* var */_bossItems: Array<any>;

        /* internal */ /* var */_passItems: Array<any>;

        /* internal */ /* var */_role: RoleItem;

        /* internal */ /* var */_target: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bossPoints: Array<any>;

        /* internal */ /* var */_showingNpc: Boolean=false;

        /* internal */ /* var */_showingTask: Boolean=true;

        /* internal */ /* var */_showingBoss: Boolean=false;

        /* internal */ /* var */_showingPass: Boolean=false;

        /* internal */ /* var */_showingArea: Boolean=false;

        /* internal */ /* var */_sceneInfo: SceneInfo;

        /* internal */ /* var */_roleX: int;

        /* internal */ /* var */_roleY: int;

        /* internal */ /* var */_targetLocation: FlashMask;

        /* internal */ /* var */_timeKey: uint;

        /* internal */ /* var */_areaItems: Array<any>;

        protected /* var */_scaleWidth: number=0;

        protected /* var */_scaleHeight: number=0;
    }
